package com.ljkj.cordial.chat.adapter;

import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.base.ChatApp;
import com.ljkj.cordial.chat.dao.UserTableBean;
import com.ljkj.cordial.chat.util.AlbumManageUtil;
import com.nic.view.adapter.CommonAdapter;
import com.nic.view.widget.CircleImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 账号切换
 */
public class ToggleAccountAdapter extends CommonAdapter<UserTableBean> {
    private SparseArray<Boolean> array = new SparseArray<>();


    @Override
    public View getBindView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        UserTableBean item = (UserTableBean) getItem(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_toggle_account, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String targetId = UserTableBean.TYPE_REGISTER.equals(item.getUserType()) ? item.getUserId() : item.getTemporaryId();
                if (targetId.equals(ChatApp.getInstance().getUserId())) {
                    return;
                }
                for (int i = 0; i < array.size(); i++) {
                    array.put(array.keyAt(i), false);
                }
                array.put(position, true);
                notifyDataSetChanged();
                if (loginOutListener != null) {
                    loginOutListener.onLoginOut(item);
                }
//                if (UserTableBean.TYPE_REGISTER.equals(item.getUserType())) {
//                    SharedUtils.put(parent.getContext(), Canstants.KEY_UseType, UserTableBean.TYPE_REGISTER);
//                    SharedUtils.put(parent.getContext(), Canstants.KEY_UserId, item.getUserId());
//                } else {
//                    SharedUtils.put(parent.getContext(), Canstants.KEY_UseType, UserTableBean.TYPE_VISITOR);
//                    SharedUtils.put(parent.getContext(), Canstants.KEY_VisitorId, item.getTemporaryId());
//                }
//                TcpLengthHeartbeatBroadcastReceive.updateUserId(ChatApp.getInstance().getUserId() + "1");
//                String visitorId = ChatApp.getInstance().get().getTemporaryId();
//                if (!TextUtils.isEmpty(visitorId)) {
//                    DeleteHistoryUtil.getInstance().deleteUuu(new DeleteHistoryUtil.OnDeleteFinishListener() {
//                        @Override
//                        public void onFinish() {
//                            ChatApp.getInstance().put(item);
//                            if (loginOutListener != null) {
//                                loginOutListener.onLoginOut(item);
//                            }
//                        }
//                    });
//                } else {
//
//                }
            }
        });
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                String targetId = UserTableBean.TYPE_REGISTER.equals(item.getUserType()) ? item.getUserId() : item.getTemporaryId();
                if (longClickListener != null && !targetId.equals(ChatApp.getInstance().getUserId())) {
                    longClickListener.onAccountLongClick(v, item, position);
                    return true;
                }
                return false;
            }
        });
        holder.tvTitle.setText(item.getNickname());
        if (UserTableBean.TYPE_REGISTER.equals(item.getUserType())) {
            holder.tvContent.setText("ID:" + item.getUserId());
            holder.ivIsVisitor.setVisibility(View.GONE);
        } else {
            holder.tvContent.setText("ID:" + item.getTemporaryId());
            holder.ivIsVisitor.setVisibility(View.VISIBLE);
        }
        boolean b;
        if (UserTableBean.TYPE_REGISTER.equals(item.getUserType()) && ChatApp.getInstance().getUserId().equals(item.getUserId())) {
            b = array.get(position, true);
        } else if (UserTableBean.TYPE_VISITOR.equals(item.getUserType()) && ChatApp.getInstance().getUserId().equals(item.getTemporaryId())) {
            b = array.get(position, true);
        } else {
            b = array.get(position, false);
        }
        if (b) {
            holder.ivSuccess.setVisibility(View.VISIBLE);
        } else {
            holder.ivSuccess.setVisibility(View.GONE);
        }
        if (!TextUtils.isEmpty(item.getAvatar())) {
            AlbumManageUtil.getInstance().downLoadFileFriend(item.getAvatar(), item.getUserId(), holder.ivAvatar);
        }else{
            holder.ivAvatar.setImageResource(R.mipmap.pic_user_avatar_default_36dp);
        }
        return convertView;
    }

    public int getSelected() {
        for (int i = 0; i < array.size(); i++) {
            boolean b = array.get(array.keyAt(i), false);
            if (b) {
                return array.keyAt(i);
            }
        }
        return 0;
    }

    class ViewHolder {
        @BindView(R.id.iv_avatar)
        CircleImageView ivAvatar;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_content)
        TextView tvContent;
        @BindView(R.id.iv_success)
        ImageView ivSuccess;
        @BindView(R.id.iv_is_visitor)
        ImageView ivIsVisitor;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    private OnLoginOutListener loginOutListener;

    public void setLoginOutListener(OnLoginOutListener loginOutListener) {
        this.loginOutListener = loginOutListener;
    }

    public interface OnLoginOutListener {
        void onLoginOut(UserTableBean newUser);
    }

    private OnAccountLongClickListener longClickListener;

    public void setLongClickListener(OnAccountLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }

    public interface OnAccountLongClickListener {
        void onAccountLongClick(View v, UserTableBean item, int position);
    }
}
